# -*- coding:utf-8 -*-

from rest_framework import serializers

from Apps.Demo.models import Demo, Person, Dog


class DemoSerializer(serializers.ModelSerializer):
    xxx = serializers.SerializerMethodField()
    # choices 通过source='get_字段名_display' 获取对应的显示字段
    gender = serializers.CharField(source='get_sex_display', read_only=True)
    # 对应字段，格式化时间
    formatTime = serializers.DateTimeField(source='ctime', format='%Y-%m-%d', read_only=True)

    xxxx = serializers.SerializerMethodField()
    cus_xxx = serializers.SerializerMethodField(method_name='get_cus_xxx', read_only=True)

    class Meta:
        model = Demo
        fields = '__all__'
        depth = 2

    # 自定义
    def get_xxx(self, obj):
        return "返回自定义的数据（万能处理）" + obj.name

    def get_xxxx(self, obj):
        return list(item.name for item in obj.person_set.all())

    def get_cus_xxx(self, obj):
        return obj.name


class PersonSerializer(serializers.ModelSerializer):
    class Meta:
        model = Person
        fields = '__all__'


class DogSerializer(serializers.ModelSerializer):
    class Meta:
        model = Dog
        fields = '__all__'
